using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Orion.Lib;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Content;
using TomShane.Neoforce.Controls;
using System.Threading;
using Microsoft.Xna.Framework.GamerServices;

namespace Orion
{
   
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class MyGame : Microsoft.Xna.Framework.Game
    {
        public static string CurrentSkin = "Default";
        public static GraphicsDeviceManager graphics;
        public static ContentManager staticContent;
        public static MyGame Instance;
        public static GameScreenBase currentScreen;
        public static SpriteFont DefaultFont;
        public static Manager UIManager;

        public MyGame()
        {
           /// Components.Add(new GamerServicesComponent(this));
            graphics = new GraphicsDeviceManager(this);
          
            graphics.PreferredBackBufferHeight = 768;
            graphics.PreferredBackBufferWidth = 1024;
            graphics.PreferMultiSampling = true;
            Content.RootDirectory = "Content";
            staticContent = Content;
            Instance = this;
           
        }       
        
        public float GetZoomFactor
        {
            get
            {
                float f = GraphicsDevice.ScissorRectangle.Width / 800;
                return f;
            }
        }
        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            this.Window.Title = "Orion - test stage";
            this.Window.AllowUserResizing = false;
            //ShowMissingRequirementMessage(new Exception("TETst"));
            this.IsMouseVisible = true;

            UIManager = new Manager(this);
            UIManager.SkinDirectory = @"Content\Skins\";
            MyGame.UIManager.Initialize();

            ThreadPool.QueueUserWorkItem(new WaitCallback((o)=>{ LevelManager.LevelList.GetType();}));

                
            // Create and assign render target for UI rendering.
           // MyGame.UIManager.RenderTarget = new RenderTarget2D(GraphicsDevice, graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight, false, SurfaceFormat.Color, DepthFormat.None, 0, RenderTargetUsage.DiscardContents);
            // Maximum of frames we want to render per second (applies only for Neoforce, not Game itself)
           // MyGame.UIManager.TargetFrames = 60; 

          
            // TODO: Add your initialization logic here
            currentScreen = new MainMenuScreen();
            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
           
            
            UIManager.SetSkin("Default");
            UIManager.AutoUnfocus = false;

            DefaultFont = staticContent.Load<SpriteFont>("default");
            currentScreen.LoadContent();
            // Create a new SpriteBatch, which can be used to draw textures.
           // spriteBatch = new SpriteBatch(GraphicsDevice);
           
            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            currentScreen.UnloadContent();

            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            currentScreen.Update(gameTime);
            MyGame.UIManager.Update(gameTime);
            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            
            //GraphicsDevice.Clear(Color.SkyBlue);
            
            MyGame.UIManager.BeginDraw(gameTime);
            currentScreen.Draw(gameTime);
           // MyGame.UIManager.Draw(gameTime);
            MyGame.UIManager.EndDraw();
            // TODO: Add your drawing code here
           
        
            base.Draw(gameTime);
        }

        Dictionary<Guid, GameScreenBase> screensCache = new Dictionary<Guid, GameScreenBase>();
        public void SwitchScreen(GameScreenBase newScr, bool disposeCurrent = true)
        {
            var oldscr = currentScreen;
            currentScreen = newScr;
            if (disposeCurrent)
            {
                if (screensCache.ContainsKey(oldscr.ID))
                    screensCache.Remove(oldscr.ID);
                oldscr.UnloadContent();
                oldscr.Dispose();
            }
            else
            {
                if (screensCache.ContainsKey(oldscr.ID))
                    screensCache[oldscr.ID] = oldscr;
                else
                    screensCache.Add(oldscr.ID, oldscr);
            }
            newScr.LoadContent();

        }
    }
}
